package com.tti.app;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.punchthrough.bean.sdk.Bean;
import com.punchthrough.bean.sdk.BeanDiscoveryListener;
import com.punchthrough.bean.sdk.BeanListener;
import com.punchthrough.bean.sdk.BeanManager;
import com.punchthrough.bean.sdk.message.BeanError;
import com.punchthrough.bean.sdk.message.Callback;
import com.punchthrough.bean.sdk.message.DeviceInfo;
import com.punchthrough.bean.sdk.message.ScratchBank;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";

    final List<Bean> beans = new ArrayList<>();

    BeanDiscoveryListener listener = new BeanDiscoveryListener() {
        @Override
        public void onBeanDiscovered(Bean bean, int rssi) {
            beans.add(bean);
            Log.e(TAG, "Name:" + bean.getDevice().getName() + " " + bean.getDevice().getAddress());

            BeanManager.getInstance().cancelDiscovery();
        }

        @Override
        public void onDiscoveryComplete() {
            // This is called when the scan times out, defined by the .setScanTimeout(int seconds) method
            Log.e("# bean #", "beans.size->" + beans.size());
            for (Bean bean : beans) {
                System.out.println(bean.getDevice().getName() + " || " + bean.getDevice().getAddress());

//                System.out.println(bean.getDevice().getAddress());  // "B4:99:4C:1E:BC:75" (example)
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        beans.clear();
        BeanManager.getInstance().setScanTimeout(30);  // Timeout in seconds, optional, default is 30 seconds
        BeanManager.getInstance().startDiscovery(listener);

    }


    @Override
    protected void onResume() {
        super.onResume();

        if (Build.VERSION.SDK_INT >= 23 && !checkLocationPermission()) {
            requestLocationPermission();
            Log.e("onResume", "Ask for Permissions");
        }
    }

    private void requestLocationPermission() {
        // Here, thisActivity is the current activity
        if (!checkLocationPermission()) {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN}, 2);
        }
    }

    private boolean checkLocationPermission() {
        return !(ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this,
                        android.Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
        );
    }

    /**
     * Connect BLE device
     */
    private void connectBLE(final Bean connectBbean) {
        // Assume we have a reference to the 'beans' ArrayList from above.
        BeanListener beanListener = new BeanListener() {
            @Override
            public void onConnected() {
                System.out.println("connected to Bean!");
                connectBbean.readDeviceInfo(new Callback<DeviceInfo>() {
                    @Override
                    public void onResult(DeviceInfo deviceInfo) {
                        System.out.println(deviceInfo.hardwareVersion());
                        System.out.println(deviceInfo.firmwareVersion());
                        System.out.println(deviceInfo.softwareVersion());
                    }
                });
            }

            @Override
            public void onReadRemoteRssi(int rssi) {

            }

            @Override
            public void onError(BeanError error) {

            }

            @Override
            public void onScratchValueChanged(ScratchBank bank, byte[] value) {

            }

            @Override
            public void onSerialMessageReceived(byte[] data) {

            }

            @Override
            public void onDisconnected() {

            }

            @Override
            public void onConnectionFailed() {

            }

        };
        // Assuming you are in an Activity, use 'this' for the context
        connectBbean.connect(this, beanListener);
    }
}
